package cn.ahaoweb.drule.core.compile.structure;

import lombok.Data;

import java.util.List;
import java.util.Map;

import static cn.ahaoweb.drule.core.compile.constant.CompileSymbols.*;

/**
 * @Name: Drule编译结构体
 * @Description: 编译为drl文件前的最终结构
 * @Author: wangzhonghao
 * @Date: 2025/2/7 14:48
 */
@Data
public class DruleCompileStructure {

    // 引入
    private List<String> imports;

    // 全局变量 k->变量名，v->变量类型
    private Map<String,String> globals;

    // 规则集合
    private List<String> rules;

    // 函数集合
    private List<String> functions;

    public String toDrl(){
        StringBuilder sb = new StringBuilder();
        if (imports != null) {
            for (String anImport : imports) {
                sb.append("import").append(blank).append(anImport).append(semicolon).append(lf);
            }
        }
        if (globals != null) {
            for (Map.Entry<String, String> entry : globals.entrySet()) {
                sb.append("global").append(blank).append(entry.getValue()).append(blank)
                        .append(entry.getKey()).append(semicolon).append(lf);
            }
        }
        if (rules != null) {
            for (String rule : rules) {
                sb.append(lf).append(rule);
            }
        }
        if (functions != null) {
            for (String function : functions) {
                sb.append(lf).append(function);
            }
        }
        return sb.toString();
    }

}
